<?php
declare (strict_types = 1);

namespace app\api\model;


use app\api\exception\ApiException;
use think\Exception;

/**
 * @mixin \think\Model
 */
class User extends \app\common\model\User
{

	public static function getUserByIdentity(string $appid = '',string $secret = ''){
		$user = User::where('appid',$appid)->findOrEmpty();
		if($user->isEmpty()){
			throw new ApiException('无效的appid');
		}

		if($user->secret != $secret){
			throw new ApiException('secret错误或者secret不属于这个账号，请确认secret的正确性');
		}

		if($user->status != self::NORMAL){
			throw new ApiException('账号已被禁用');
		}
		return $user;
	}

	public static function getUserByAppid(string $appid = ''){
		$user = User::where('appid',$appid)->findOrEmpty();
		if($user->isEmpty()){
			throw new ApiException('无效的appid');
		}


		if($user->status != self::NORMAL){
			throw new ApiException('账号已被禁用');
		}
		return $user;
	}

    public static function chkIp(string $ip,\app\common\model\User $user){
        if(empty($user->ip_list)){
            return true;
        }
        $ipList = explode("\r\n",$user->ip_list);
        if(!in_array($ip,$ipList)){
            throw new ApiException('调用接口的IP地址不在白名单中，请在接口IP白名单中进行设置。');
        }
        return true;
    }
}
